package com.lecture.junit.tdd.example;

import com.lecture.junit.tdd.example.exception.PasswordWasAlreadyUsedException;

import java.util.ArrayList;

/**
 * User: Гри
 */
public class LastPasswordsUsed {

    private final int rememberCapacity;

    private final ArrayList<String> passwords;

    public LastPasswordsUsed(int rememberCapacity) {
        this.rememberCapacity = rememberCapacity;
        passwords = new ArrayList<>(rememberCapacity);
    }

    public synchronized void add(String password) throws PasswordWasAlreadyUsedException {
        if(passwords.contains(password)){
            throw new PasswordWasAlreadyUsedException("Password was already used earlier! Specify other");
        }else if (passwords.size() == rememberCapacity){
            passwords.remove(0);
        }
        passwords.add(password);
    }

    public synchronized boolean contains(String password){
        return passwords.contains(password);
    }

}
